package com.lekang.le.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lekang.common.annotation.Anonymous;
import com.lekang.le.domain.ElderlyBedVO;
import com.lekang.le.domain.ElderlySelectDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.le.domain.ElderlySzh;
import com.lekang.le.service.IElderlySzhService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 老人管理Controller
 * 
 * @author szh
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/le/elderly")
@CrossOrigin
public class ElderlySzhController extends BaseController
{
    @Autowired
    private IElderlySzhService elderlySzhService;
    // 新增接口：根据老人ID查询单条完整信息（用于ptList跳转index1）
    @PreAuthorize("@ss.hasPermi('le:elderly:query')")  // 与listAll接口权限一致
    @GetMapping("/selectById/{elderlyId}")  // 路径与参数格式模仿listAll
    public AjaxResult getElderlyByElderlyId(@PathVariable("elderlyId") Long elderlyId) {
        // 调用Service层新增方法，返回单条老人信息
        ElderlySelectDTO elderly = elderlySzhService.getElderlyByElderlyId(elderlyId);
        return success(elderly);  // 与listAll接口响应格式一致
    }
    /**
     * 获取老人选择弹窗数据列表（支持分页）
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:selectList')") // 权限标识与业务匹配
    @GetMapping("/selectList")
    public TableDataInfo selectList() {
        startPage(); // 若依分页工具：自动从请求参数中获取pageNum、pageSize进行分页
        List<ElderlySelectDTO> list = elderlySzhService.getElderlySelectList(); // 调用Service获取数据
        return getDataTable(list); // 若依表格数据统一响应格式（包含total、rows等字段）
    }
    /**
     * 查询老人管理列表
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElderlySzh elderlySzh)
    {
        startPage();
        List<ElderlySzh> list = elderlySzhService.selectElderlySzhList(elderlySzh);
        return getDataTable(list);
    }

    /**
     * 查询老人联系信息列表
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:query')")
    @GetMapping("/list/{id}")
    public AjaxResult listAll(@PathVariable("id") Long elderlyId)
    {
        ElderlySzh elderlySzh = elderlySzhService.selectElderlySzhAllById(elderlyId);
        return success(elderlySzh);
    }

    /**
     * 获取老人管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(elderlySzhService.selectElderlySzhById(id));
    }


    /**
     * 导出老人管理列表
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:export')")
    @Log(title = "老人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElderlySzh elderlySzh)
    {
        List<ElderlySzh> list = elderlySzhService.selectElderlySzhList(elderlySzh);
        ExcelUtil<ElderlySzh> util = new ExcelUtil<ElderlySzh>(ElderlySzh.class);
        util.exportExcel(response, list, "老人管理数据");
    }


    /**
     * 新增老人管理
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:add')")
    @Log(title = "老人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElderlySzh elderlySzh)
    {
        // 调用 Service 层，获取影响行数
        int rows = elderlySzhService.insertElderlySzh(elderlySzh);
        if (rows > 0) {
            // 插入成功：从实体类中获取自增 id，封装到响应中
            return AjaxResult.success("新增成功", elderlySzh.getId());
        } else {
            // 插入失败：返回错误信息
            return AjaxResult.error("新增失败，请检查数据");
        }
    }

    /**
     * 修改老人管理
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:edit')")
    @Log(title = "老人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElderlySzh elderlySzh)
    {
        return toAjax(elderlySzhService.updateElderlySzh(elderlySzh));
    }

    /**
     * 删除老人管理
     */
    @PreAuthorize("@ss.hasPermi('le:elderly:remove')")
    @Log(title = "老人管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(elderlySzhService.deleteElderlySzhByIds(ids));
    }
    /**
     * 小程序“家人”页面：根据登录用户ID查询绑定的老人及床位信息
     * @param wxUserId 登录用户全局ID（从小程序登录态中获取）
     * @return 老人信息列表
     */
    @Anonymous
    @GetMapping("/list/family")
    public AjaxResult getElderlyFamilyList(@RequestParam Long wxUserId)
    {
        List<ElderlyBedVO> elderlyBedVOS = elderlySzhService.selectElderlyByWxUserId(wxUserId);
        return success(elderlyBedVOS);
    }
}
